package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.PledgeTaskInfoDao;
import com.sc.nft.dao.UserCollectionDao;
import com.sc.nft.dao.UserVigorousCollectionRecordsDao;
import com.sc.nft.dao.UserVigorousRecordsDao;
import com.sc.nft.entity.PledgeTaskInfo;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.UserVigorousCollectionRecords;
import com.sc.nft.entity.UserVigorousRecords;
import com.sc.nft.entity.vo.AdmiVigorousCollectionVO;
import com.sc.nft.entity.vo.AdminPageVigorousRecordVO;
import com.sc.nft.entity.vo.VigorousSecretAreaDetailsVO;
import com.sc.nft.entity.vo.VigorousSecretAreaPageVO;
import com.sc.nft.enums.VigorousStatusEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.PledgeTaskInfoService;
import com.sc.nft.service.UserCollectionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.UserVigorousCollectionRecordsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 质押任务-用户激活记录服务接口实现
 *
 * @author Sun
 * @since 2023-06-12 15:37:12
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserVigorousCollectionRecordsServiceImpl implements UserVigorousCollectionRecordsService {
    private final UserVigorousCollectionRecordsDao userVigorousCollectionRecordsDao;
    private final UserVigorousRecordsDao userVigorousRecordsDao;
    private final PledgeTaskInfoDao pledgeTaskInfoDao;
    private final UserCollectionService userCollectionService;
    @Override
    public Page<AdminPageVigorousRecordVO> adminPageVigorousRecord(Long pledgeTaskId,String userTel, Integer pageNo, Integer pageSize) {
        return userVigorousRecordsDao.adminPageVigorousRecord(pledgeTaskId,userTel, pageNo, pageSize);
    }

    @Override
    public List<AdmiVigorousCollectionVO> adminVigorousCollectionRecord(Long userVigorousRecordsId) {
        return userVigorousCollectionRecordsDao.adminVigorousCollectionRecord(userVigorousRecordsId);
    }

    @Override
    public Integer getOneByUserIdAndPledgeTaskIdAndVigorousStatus(Long userId, Long pledgeTaskId, VigorousStatusEnum vigorousStatus) {
        return userVigorousRecordsDao.getOneByUserIdAndPledgeTaskIdAndVigorousStatus(userId, pledgeTaskId, vigorousStatus);
    }

    @Override
    public UserVigorousRecords getOneByPledgeTaskIdAndUserId(Long userId, Long pledgeTaskId, VigorousStatusEnum vigorousStatus) {
        return userVigorousRecordsDao.getOneByPledgeTaskIdAndUserId(userId, pledgeTaskId, vigorousStatus);
    }

    @Override
    public List<UserVigorousCollectionRecords> getListByUserVigorousRecordsId(Long userVigorousRecordsId, VigorousStatusEnum vigorousStatus) {
        return userVigorousCollectionRecordsDao.getListByUserVigorousRecordsId(userVigorousRecordsId, vigorousStatus);
    }

    @Override
    public Page<VigorousSecretAreaPageVO> getPageVigorousSecretAreaRecordsByUserId(Long userId, Integer pageNo, Integer pageSize) {
        return userVigorousCollectionRecordsDao.getPageVigorousSecretAreaRecordsByUserId(new Page<>(pageNo,pageSize),userId);
    }

    @Override
    public VigorousSecretAreaDetailsVO vigorousSecretAreaDetails(UserInfo user, Long pledgeTaskId,Long userVigorousRecordsId) {
        //校验质押任务
        PledgeTaskInfo pledgeTaskInfo = pledgeTaskInfoDao.getById(pledgeTaskId);
        Assert.notNull(pledgeTaskInfo,()->new GlobalRunTimeException("秘境不存在"));

        UserVigorousRecords userVigorousRecords = userVigorousRecordsDao.getOneByPledgeTaskIdAndUserIdAndId(user.getId(), pledgeTaskId,userVigorousRecordsId);
        VigorousSecretAreaDetailsVO vigorousSecretAreaDetailsVO = BeanUtil.copyProperties(userVigorousRecords, VigorousSecretAreaDetailsVO.class);
        vigorousSecretAreaDetailsVO.setPledgeTaskEndTime(pledgeTaskInfo.getPledgeTaskEndTime());
        vigorousSecretAreaDetailsVO.setPledgeTaskName(pledgeTaskInfo.getPledgeTaskName());
        if(userVigorousRecords.getVigorousStatus() == VigorousStatusEnum.RETURNED){
            vigorousSecretAreaDetailsVO.setPledgeClosedTime(userVigorousRecords.getModifyTime());
        }
        vigorousSecretAreaDetailsVO.setUserVigorousCollectionList(userVigorousCollectionRecordsDao.getUserVigorousCollectionListByUserVigorousRecordsId(userVigorousRecords.getId()));

        return vigorousSecretAreaDetailsVO;
    }

    @Override
    public UserVigorousRecords getLastOneByUserIdAndPledgeTaskId(Long userId, Long pledgeTaskId) {
        return userVigorousRecordsDao.getOneByPledgeTaskIdAndUserId(userId, pledgeTaskId, null);
    }
}